#include "stdafx.h"
#include "stdio.h"
#include "List.h"
#include "DataEngine.h"
#include "CumRoomOrder.h"
#include "string"
#include "sstream"
using namespace std;
extern DataEngine* g_pEg;

CumRoomOrder::CumRoomOrder()
{

}

int CumRoomOrder::SetData(char* name,int sex,char* pTelNO,int iAge,char* pRoomID,char* pCumCardID,int iInDays,double price)
{
	m_pName=name;
	m_iSex=sex;
	m_pTelNo=pTelNO;
	m_iAge=iAge;
	m_pRoomID=pRoomID;
	m_pCardID=pCumCardID;
	m_iInDays=iInDays;
	m_fPrice=price;
	return 0;
}

int CumRoomOrder::AddRecord()
{
	char sqlquery[1024] = { '\0' };
	/*string sql = string("SELECT * FROM `hm_member` WHERE `cardid`='") + string(m_pCardID) + string("' limit 1");
	DataRecord* pClientDataRecord = g_pEg->DataQuery(sql.c_str());
	if (pClientDataRecord){
		int recordCount = pClientDataRecord->GetRecordCount();
		if (!recordCount)
		{
			stringstream sex,age;
			sex << m_iSex;
			age << m_iAge;
			string sql_member = string("INSERT INTO `hm_member` (`cardid`,`name`,`sex`,`age`) VALUES ('")
				+ string(m_pCardID)
				+ string("','")
				+ string(m_pName) + string("','")
				+ string(sex.str())
				+ string("','")
				+ string(age.str())
				+ string("')");
			g_pEg->IoQuery(sql_member.c_str());
		}
		else
		{
			pClientDataRecord->GetNextRecord();
			AutoVar * av_client_id = pClientDataRecord->GetValueByName("id");
			int client_id = *(int*)av_client_id->ptr;

			sprintf(sqlquery, "INSERT INTO `hm_news` SET `content`='FirstFromProgram',`title`='TextTitle'");
			g_pEg->IoQuery(sqlquery);
		}
	}*/
	stringstream sex, age , smid;
	sex << m_iSex;
	age << m_iAge;
	string sql_member = string("INSERT INTO `hm_member` (`cardid`,`name`,`sex`,`age`) VALUES ('")
		+ string(m_pCardID)
		+ string("','")
		+ string(m_pName) + string("','")
		+ string(sex.str())
		+ string("','")
		+ string(age.str())
		+ string("')");
	int rows=g_pEg->IoQuery(sql_member.c_str());
	if (rows == 0)
		return FALSE;
	int mid = g_pEg->Get_Last_Insert_ID();
	smid << mid;
	string sql_order = string("INSERT INTO `hm_order` SET `mid`=") + string(smid.str())+string(",``");
	rows = g_pEg->IoQuery(sql_order.c_str());
	return TRUE;
}

CumRoomOrder::~CumRoomOrder()
{
	delete m_pCardID;
	delete m_pName;
	delete m_pRoomID;
	delete m_pTelNo;
}
int CumRoomOrderCollect::Save()
{
	List<CumRoomOrder>* pOrders=m_pOrders;
	if(pOrders==NULL)
		return NULL;
	int gflag=TRUE;
	CumRoomOrder* pOrder=pOrders->Head();
	do
	{		
		if(pOrder!=NULL)
		{
			pOrder->AddRecord();
			pOrder=pOrders->Next();
		}
		else
		{
			gflag=FALSE;
		}		
	}while(gflag!=FALSE);
	delete m_pOrders;
	m_pOrders=NULL;
	return 0;
}

int CumRoomOrderCollect::Add(CumRoomOrder* pOrder)
{
	List<CumRoomOrder>* psOrder=NULL;
	if(m_pOrders==NULL)
		m_pOrders=new List<CumRoomOrder>();
	psOrder=m_pOrders;

	psOrder->Append(pOrder);
	return 0;
}

int CumRoomOrderCollect::Clear()
{
	if(m_pOrders!=NULL)
	{
		CumRoomOrder* pOrder=m_pOrders->Head();
		while(pOrder!=NULL)
		{
			delete pOrder;
			pOrder=m_pOrders->Next();	
		}
	}
	return 0;
}

int CumRoomOrderCollect::Delete(int index)
{
	CumRoomOrder* pOrder=m_pOrders->Head();
	int i=0;
	while(pOrder!=NULL)
	{
		if(i==index)
		{
			m_pOrders->Delete(pOrder);
			return 0;
		}
		else
		{
			i++;
			pOrder=m_pOrders->Next();
		}
	}
	return 0;
}


CumRoomOrderCollect::~CumRoomOrderCollect()
{
	delete m_pOrders;
	m_pOrders=NULL;
}

CumRoomOrderCollect::CumRoomOrderCollect():m_pOrders(NULL)
{
}

int CumRoomOrderCollect::Count()
{
	int i = 0;
	CumRoomOrder* pOrder=m_pOrders->Head();
	if (pOrder)
		i = 1;
	while (m_pOrders->Next())
	{
		i++;
	}
	return i;
}

